<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/11/29
  Time: 8:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        function creatXMLHttpRequest() {
            var xmlHttp;
            try {
                xmlHttp = new XMLHttpRequest();
            }catch (e){
                try{
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                }catch (e){
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
            }
            return xmlHttp;
        }
        function chekUsername(){
            var xmlHTTP = creatXMLHttpRequest();
            var username = document.getElementById("username").value;
            xmlHTTP.open("POST","/ajax/findUsernameServlet?username="+username,true);
            xmlHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHTTP.send("username="+username+"&time="+new Date());
            xmlHTTP.onreadystatechange = function () {
                var msg = document.getElementById("msg")
                if (xmlHTTP.readyState == 4 && xmlHTTP.status==200){
                    var flag = xmlHTTP.responseText;
                    if (flag == "true"){
                        msg.innerText="用户已经存在";
                        msg.style.color = "gray"
                    } else {
                        msg.innerText="用户名可注册"
                        msg.style.color="green"
                    }
                }
            }
        }
    </script>
</head>
<body>
<form action="">
    用户名：<input type="text" name="username" value="" onblur="chekUsername()"><span id="msg"></span>
    <br>
    密码：<input type="password" name="password" value="">
</form>
</body>
</html>
